/* Copyright (C) 2008 Sahid Ferdjaoui <sahid@funraill.org>
 *
 *   This file is part of Postmemd.
 *
 * Postmemd is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "pm_table.h"
#include "pm_log.h"

#include "uthash.h"

#include <sys/types.h>
#include <string.h>
#include <stdlib.h>


static table_t * tables;


int 
pm_table_init (table_t ** table)
{
	( * table) = malloc (sizeof (table_t));
	if (( * table) == NULL)
		{
			pm_log_error ("pm_table_init () - malloc ()", PM_PRINT_ERRNO);
			return -1;
		}
	return 0;
}

pid_t
pm_table_get_pid (char key[])
{
	table_t * table = NULL;
	pid_t pid = -1;

	HASH_FIND_STR (tables, key, table);
	if (table)
		pid = table->pid;
	return pid;
}

void
pm_table_add_pid (pid_t pid_slave, char key[])
{
 	table_t * table = NULL;

	pm_table_init (& table);

	strcpy (table->key, key);
	table->pid = pid_slave;

	HASH_ADD_STR (tables, key, table);
}

void
pm_table_del_pid (char key[])
{
	table_t * table = NULL;

	pm_table_init (& table);
	
	strcpy (table->key, key);
	HASH_DEL (tables, table);
}
